package com.pz.gulimall.auth.controller;

import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaResult;
import com.pz.gulimall.auth.service.LoginService;
import com.pz.gulimall.auth.vo.req.UserLoginReqVo;
import com.pz.gulimall.common.exception.BizCodeEnum;
import com.pz.gulimall.common.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 客户端登录
 */
@RestController
public class LoginController {
    @Autowired
    private LoginService loginService;

    /**
     * 单点登录
     */
    @PostMapping("/login")
    public R login(@RequestBody @Valid UserLoginReqVo vo){
        return loginService.singleSingOn(vo);
    }

    @RequestMapping("isLogin")
    public R isLogin() {
        return StpUtil.isLogin() ?
                R.ok(BizCodeEnum.AUTH_LOGGING_STATUS.getMsg()) :
                R.error(BizCodeEnum.AUTH_LOGOUT_STATUS.getCode(),BizCodeEnum.AUTH_LOGOUT_STATUS.getMsg());
    }

    @GetMapping("/logout")
    public SaResult logout() {
        StpUtil.logout();
        return SaResult.ok();
    }
}
